package com.richman.utils;


import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
public class Common {
	
	public static boolean isNum(String str){
        try {
            new BigDecimal(str);
            return true;
        } catch (Exception e) {
            return false;
        }
	}
	
	
	public static String pankouStr(String str){
		
		try{
			
			
			if(str != null && !str.equals("")){
				
				if(str.contains("H")) {
					str = str.replace("H", "");
					str = str.replace(" ", "");
					
					if(str.contains("/")) {
						double r1 = Double.parseDouble(str.split("/")[0]);
						double r2 = Double.parseDouble(str.split("/")[1]);
						return Double.toString((r1+r2)/2);
					}else {
						return str;
					}
					
				}
				
				if(str.contains("C")) {
					str = str.replace("C", "");
					str = str.replace(" ", "");
					
					if(str.contains("/")) {
						double r1 = Double.parseDouble(str.split("/")[0]);
						double r2 = Double.parseDouble(str.split("/")[1]);
						return Double.toString(0.0 - (r1+r2)/2);
					}else {
						return "-" + str;
					}
					
				}
				
				
				
				if(str.contains("O")) {
					str = str.replace("O", "");
					str = str.replace(" ", "");
					
					if(str.contains("/")) {
						double r1 = Double.parseDouble(str.split("/")[0]);
						double r2 = Double.parseDouble(str.split("/")[1]);
						return Double.toString((r1+r2)/2);
					}else {
						return str;
					}
				}
				
				
				
				
				

			}else {
				return "";
			}
			
		}catch(Exception e){
			e.printStackTrace();
			
		}
		
		return "";
	}
	
	
	 public static Object getValue(String param) {
	        Map map = new HashMap();
	        String str = "";
	        String key = "";
	        Object value = "";
	        char[] charList = param.toCharArray();
	        boolean valueBegin = false;
	        for (int i = 0; i < charList.length; i++) {
	            char c = charList[i];
	            if (c == '{') {
	                if (valueBegin == true) {
	                    value = getValue(param.substring(i, param.length()));
	                    i = param.indexOf('}', i) + 1;
	                    map.put(key, value);
	                }
	            } else if (c == '=') {
	                valueBegin = true;
	                key = str;
	                str = "";
	            } else if (c == ',') {
	                valueBegin = false;
	                value = str;
	                str = "";
	                map.put(key, value);
	            } else if (c == '}') {
	                if (str != "") {
	                    value = str;
	                }
	                map.put(key, value);
	                return map;
	            } else if (c != ' ') {
	                str += c;
	            } else if(c == ' ') {
	            	str += c;
	            }
	        }
	        return map;
	    }
	
	
	public static Double pankouDouble(String str){
		
		try{
			
			
			if(str != null && !str.equals("")){
				
				if(str.contains("H")) {
					str = str.replace("H", "");
					str = str.replace(" ", "");
					
					if(str.contains("/")) {
						double r1 = Double.parseDouble(str.split("/")[0]);
						double r2 = Double.parseDouble(str.split("/")[1]);
						return (r1+r2)/2;
					}else {
						return Double.parseDouble(str);
					}
					
				}
				
				else if(str.contains("C")) {
					str = str.replace("C", "");
					str = str.replace(" ", "");
					
					if(str.contains("/")) {
						double r1 = Double.parseDouble(str.split("/")[0]);
						double r2 = Double.parseDouble(str.split("/")[1]);
						return 0.0 - (r1+r2)/2;
					}else {
						return 0.0 - Double.parseDouble(str);
					}
					
				}
				
				
				
				else{
					
					str = str.replace(" ", "");
					
					if(str.contains("/")) {
						double r1 = Double.parseDouble(str.split("/")[0]);
						double r2 = Double.parseDouble(str.split("/")[1]);
						return (r1+r2)/2;
					}else {
						return Double.parseDouble(str);
					}
				}
				
				
				
				
				

			}else {
				return 0.0;
			}
			
		}catch(Exception e){
			e.printStackTrace();
			
		}
		
		return 0.0;
	}
	
	
	public static String getcurrentTimeStr() {
		
		try {
			
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			return df.format(System.currentTimeMillis());
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		return "";
		
	}
	
	
	public static String showRatio(String ratio) {
		if(Common.isNum(ratio)) {
			
			if(Double.parseDouble(ratio) > 1.0) {
				return String.format("%.3f", (Double.parseDouble(ratio) - 2.0));
			}
			
		}
		
		return ratio;
	}
	
	
	
	public static void logOut(String str) {
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		System.out.println(df.format(System.currentTimeMillis()) + "   :"   + str);
		
	}
	

}

